package io.jee.caisan.weixin.controller.admin;

import javax.annotation.Resource;

import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import io.jee.alaska.data.page.PageInputDataTable;
import io.jee.alaska.data.page.PageOutputDataTable;
import io.jee.alaska.data.page.PageUtils;
import io.jee.alaska.util.Result;
import io.jee.caisan.weixin.domain.SysConfig;
import io.jee.caisan.weixin.repository.SysConfigRepository;
import io.jee.caisan.weixin.service.SysConfigService;

@Controller
@RequestMapping("/admin/sys/config")
public class SysConfigController {

	@Resource
	private SysConfigService sysConfigService;
	
	@Resource
	private SysConfigRepository sysConfigRepository;
	
    @GetMapping
    public String index(Model model){
    	model.addAttribute("types", SysConfig.allTypes);
    	return "admin/sys/config";
    }
    
    @GetMapping("/add")
    public String add(Model model){
    	model.addAttribute("types", SysConfig.allTypes);
        return "admin/sys/config-add";
    }
    
    @GetMapping("/modify/{id}")
    public String modify(@PathVariable Integer id, Model model){
    	model.addAttribute("types", SysConfig.allTypes);
        model.addAttribute("sysConfig", sysConfigRepository.getOne(id));
        return "admin/sys/config-modify";
    }
    
    @PostMapping("/page")
    @ResponseBody
    public PageOutputDataTable<SysConfig> page(String type, PageInputDataTable page){
        Pageable pageable = page.toPageRequest();
        return PageUtils.toPageDataTable(sysConfigService.findPage(type, pageable));
    }
    
    @PostMapping("add")
    @ResponseBody
    public Result<SysConfig> add(SysConfig sysConfig){
    	return sysConfigService.add(sysConfig);
    }
    
    @PostMapping("update")
    @ResponseBody
    public Result<SysConfig> update(SysConfig sysConfig){
    	return sysConfigService.update(sysConfig);
    }
    
    @PostMapping("delete")
    @ResponseBody
    public Result<Void> delete(Integer[] ids){
    	return sysConfigService.delete(ids);
    }
    
    @PostMapping("update/enable")
    @ResponseBody
    public Result<Integer> updateEnable(Integer id, Boolean enable){
    	return sysConfigService.updateEnable(id, enable == null ? false : enable);
    }
}
